﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EXEStart
{
    public partial class frmPropety : Form
    {
        public int index { get; set; } = -1;


        public frmPropety(GameItem.ItemInfo itemInfo = null)
        {
            this.itemInfo = itemInfo;
            InitializeComponent();
        }

        public GameItem.ItemInfo itemInfo { get; }
        public string ImagePath { get; }
        Image image;
        private void frmPropety_Load(object sender, EventArgs e)
        {

            if(itemInfo == null)
            {
                this.Icon = GetFileIcon.GetIcon(Application.ExecutablePath); //global::EXEStart.Properties.Resources.Game;
                MessageBox.Show(this, "打开错误：参数为空！", "属性", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Dispose(true);
                return;
            }

            this.Text = itemInfo.name + " 属性";
            if(!string.IsNullOrEmpty(itemInfo.image))
            {
                image = GameItem.Base64ToImg(itemInfo.image);
                picIcon64.Image = image;
                Icon = GetFileIcon.ImageToIcon(image, 32, 32);
            }
            txtName.Text = itemInfo.name;
            txtPath.Text = itemInfo.path;
            txtArgs.Text = itemInfo.args;
            txtWorkDir.Text = itemInfo.workingDir;
            cmbWinStyle.SelectedIndex = itemInfo.windowsSytle > 0 ? itemInfo.windowsSytle - 1 : 0;

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Visible = false;
            this.Close();
            this.Dispose(true);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            setItemValue();

            this.Visible = false;
        }


        private void setItemValue()
        {
            itemInfo.name = txtName.Text;
            itemInfo.path = txtPath.Text;
            itemInfo.args = txtArgs.Text;
            //itemInfo.image = GameItem.imgToBase64(image);
            itemInfo.workingDir = txtWorkDir.Text;
            itemInfo.windowsSytle = cmbWinStyle.SelectedIndex < 1 ? 0 : cmbWinStyle.SelectedIndex + 1;

        }

        private int getWinStyle(int style)
        {
            if(style == 0)
            {
                return style;
            }
            return style - 1;
        }

        private void btnOpenDir_Click(object sender, EventArgs e)
        {
            GameItem.RunEXE(this, itemInfo, GameItem.RunType.Directory);
        }

        private string extStr = ".exe|.dll|.ico";

        private void btnChangeICO_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog()
            {
                FileName = itemInfo.path,
                Filter = "程序文件(*.exe;*.dll)|*.exe;*.dll|图像文件(*.ico;*.jpg;*.png)|*.ico;*.jpg;*.png",
                Multiselect = false,
                InitialDirectory = itemInfo.workingDir,
            };

            var rst = openFileDialog.ShowDialog();

            if(rst == DialogResult.Cancel) { return; }

            string icofile = openFileDialog.FileName;
            if(string.IsNullOrEmpty(icofile))
            {
                return;
            }
            FileInfo fileInfo = new FileInfo(icofile);
            string ext = fileInfo.Extension.ToLower();

            if(extStr.Contains(ext))
            {
                Icon icon = GetFileIcon.GetIcon(icofile, 0, IconSize.Jumbo);
                if(icon != null)
                {
                    image = icon.ToBitmap();
                }
            }
            else
            {
                image = Image.FromFile(icofile);
            }

            //image = GetFileIcon.ResizeImage(image, 100);
            picIcon64.Image = image;
            itemInfo.image = GameItem.imgToBase64(image);
            itemInfo.ImageICO=(Image)image.Clone();

        }

    }
}
